********************************************************************************
*		
*						Patacchini, Pin, Rotesi (2023) 
*		Information Transmission in a Social Network: A Field Experiment
*           	
*						MASTER PROGRAM FOR REPLICATION
*
********************************************************************************

clear all
set seed 12495

********************************************************************************
* Working directories
********************************************************************************

* Please set your directory *
global path <PATH>

global data		   "$path/Data"
global results	   "$path/Results"
global temp	   	   "$path/tmp"


cd "$path"	
set more off

use "$data/Individual_level"

merge m:1 character using "$data/Group_level"

drop _merge

********************************************************************************
********************************************************************************
* Data Preparation
********************************************************************************
********************************************************************************

* Generate variables

* Here we define other_side_right by looking at how often
* the hints received by A or B travel to D (or viceversa)

egen man_id = group(character)
egen pos_id = group(pos)

preserve

keep hint_cloth man_id pos_id
reshape wide hint_cloth, i(man_id) j(pos_id) 
save temp_pos.dta,replace
restore

merge m:1 man_id using temp_pos.dta
drop _merge
rm temp_pos.dta

gen other_side_right = 0
*replace if a or b got d's right
*which is, if they got right the position in hint_cloth4
replace other_side_right=1 if (pos_id == 1 | pos_id==2) & ///
						((hint_cloth4=="cappello" & score_hat==10) | ///
						(hint_cloth4=="felpa" & score_shirt==10) | ///
						(hint_cloth4=="pantaloni" & score_pant==10) | ///
						(hint_cloth4=="scarpe" & score_shoe==10) | ///
						(hint_cloth4=="guanti" & score_glove==10))
*this is 1 if D got A right
replace other_side_right=1 if (pos_id == 4) & ///
						((hint_cloth1=="cappello" & score_hat==10) | ///
						(hint_cloth1=="felpa" & score_shirt==10) | ///
						(hint_cloth1=="pantaloni" & score_pant==10) | ///
						(hint_cloth1=="scarpe" & score_shoe==10) | ///
						(hint_cloth1=="guanti" & score_glove==10))
*this is 1 if D got B right
replace other_side_right=1 if (pos_id == 4) & ///
						((hint_cloth2=="cappello" & score_hat==10) | ///
						(hint_cloth2=="felpa" & score_shirt==10) | ///
						(hint_cloth2=="pantaloni" & score_pant==10) | ///
						(hint_cloth2=="scarpe" & score_shoe==10) | ///
						(hint_cloth2=="guanti" & score_glove==10))

* Here we define other_side_right by looking at how often
* E's hint reaches the others

gen node_got_E_right = 0

replace node_got_E_right = 1 if (hint_cloth5=="cappello" & score_hat==10) | ///
						(hint_cloth5=="felpa" & score_shirt==10) | ///
						(hint_cloth5=="pantaloni" & score_pant==10) | ///
						(hint_cloth5=="scarpe" & score_shoe==10) | ///
						(hint_cloth5=="guanti" & score_glove==10)
						
* Number of correct guesses by position
gen a_correct = 0
replace a_correct = num_corrects if pos=="a"

gen b_correct = 0
replace b_correct = num_corrects if pos=="b"

gen c_correct = 0
replace c_correct = num_corrects if pos=="c"

gen d_correct = 0
replace d_correct = num_corrects if pos=="d"

gen e_correct = 0
replace e_correct = num_corrects if pos=="e"

* Win by position
gen a_win = 0
replace a_win = winner if pos=="a"

gen b_win = 0
replace b_win = winner if pos=="b"

gen c_win = 0
replace c_win = winner if pos=="c"

gen d_win = 0
replace d_win = winner if pos=="d"

gen e_win = 0
replace e_win = winner if pos=="e"

* Exchange between classes (two different classes)
gen ab_diff_class = 0
replace ab_diff_class = 1 if ab_same_class==0

gen be_diff_class = 0
replace be_diff_class = 1 if be_same_class==0

gen cd_diff_class = 0
replace cd_diff_class = 1 if cd_same_class==0

gen ce_diff_class = 0
replace ce_diff_class = 1 if ce_same_class==0

gen de_diff_class = 0
replace de_diff_class = 1 if de_same_class==0

* Generate variable to say how often the No info exch or asymmetric info result 
* in grey  or mistakes
foreach i of num  1/5 {

generate put_`i'_grey = 0 
replace put_`i'_grey = 1 if (hint_cloth`i'=="cappello" & score_hat==0) | ///
						(hint_cloth`i'=="felpa" & score_shirt==0) | ///
						(hint_cloth`i'=="pantaloni" & score_pant==0) | ///
						(hint_cloth`i'=="scarpe" & score_shoe==0) | ///
						(hint_cloth`i'=="guanti" & score_glove==0)

}

gen A_put_B_grey = 0 
replace A_put_B_grey =1  if put_2_grey==1 & pos_id==1 

gen B_put_A_grey = 0
replace B_put_A_grey =1  if put_1_grey==1 & pos_id==2 

gen B_put_E_grey = 0
replace B_put_E_grey =1  if put_5_grey==1 & pos_id==2 

gen E_put_B_grey = 0
replace E_put_B_grey =1  if put_2_grey==1 & pos_id==5 

gen C_put_D_grey = 0
replace C_put_D_grey =1  if put_4_grey==1 & pos_id==3 

gen D_put_C_grey = 0
replace D_put_C_grey =1  if put_3_grey==1 & pos_id==4 

gen C_put_E_grey = 0
replace C_put_E_grey =1  if put_5_grey==1 & pos_id==3 

gen E_put_C_grey = 0
replace E_put_C_grey =1  if put_3_grey==1 & pos_id==5 

gen D_put_E_grey = 0
replace D_put_E_grey =1  if put_5_grey==1 & pos_id==4 

gen E_put_D_grey = 0
replace E_put_D_grey =1  if put_3_grey==1 & pos_id==5 

*collapse by group and keep the max
preserve
keep if  grp_act_num==5				

sort phase character
collapse (max) A_put_B_grey B_put_A_grey B_put_E_grey E_put_B_grey C_put_D_grey D_put_C_grey C_put_E_grey E_put_C_grey D_put_E_grey E_put_D_grey ab_same_class be_same_class cd_same_class ce_same_class de_same_class ab_diff_class be_diff_class cd_diff_class ce_diff_class de_diff_class de De dE DE cd Cd cD CD ce Ce cE CE be Be bE BE ab Ab aB AB, by(phase character)

quietly by phase character:  gen dup = cond(_N==1,0,_n)
drop if dup>1

sort phase character


gen exch_between = .
replace exch_between = (ab_diff_class*AB + be_diff_class*BE + cd_diff_class*CD + ///
						ce_diff_class*CE + de_diff_class*DE) / (ab_diff_class +  ///
						be_diff_class + cd_diff_class + ce_diff_class + de_diff_class) 

gen asy_exch_between = .
replace asy_exch_between = (ab_diff_class*aB + ab_diff_class*Ab + be_diff_class*Be + be_diff_class*bE ///
						+ cd_diff_class*cD + cd_diff_class*Cd + ce_diff_class*cE + ce_diff_class*Ce ///
						+ de_diff_class*De + de_diff_class*dE) / (ab_diff_class +  ///
						be_diff_class + cd_diff_class + ce_diff_class + de_diff_class)
										

gen asy_exch_between_grey = .
replace asy_exch_between_grey = (ab_diff_class*aB*A_put_B_grey + ab_diff_class*Ab*B_put_A_grey ///
						+ be_diff_class*Be*E_put_B_grey + be_diff_class*bE*B_put_E_grey ///
						+ cd_diff_class*cD*C_put_D_grey + cd_diff_class*Cd*D_put_C_grey ///
						+ ce_diff_class*cE*C_put_E_grey + ce_diff_class*Ce*E_put_C_grey ///
						+ de_diff_class*De*E_put_D_grey + de_diff_class*dE*D_put_E_grey) ///
						/ (ab_diff_class + be_diff_class + cd_diff_class + ce_diff_class + de_diff_class)

gen no_exch_between = .
replace no_exch_between = (ab_diff_class*ab + be_diff_class*be + cd_diff_class*cd + ///
						ce_diff_class*ce + de_diff_class*de) / (ab_diff_class +  ///
						be_diff_class + cd_diff_class + ce_diff_class + de_diff_class) 

						
gen no_exch_between_1grey = .
replace no_exch_between_1grey = (ab_diff_class*ab*A_put_B_grey*(1-B_put_A_grey) ///
						+ ab_diff_class*ab*B_put_A_grey*(1-A_put_B_grey) ///
						+ be_diff_class*be*B_put_E_grey*(1-E_put_B_grey) ///
						+ be_diff_class*be*(1-B_put_E_grey)*E_put_B_grey ///
						+ cd_diff_class*cd*C_put_D_grey*(1-D_put_C_grey) /// 
						+ cd_diff_class*cd*(1-C_put_D_grey)*D_put_C_grey /// 
						+ ce_diff_class*ce*C_put_E_grey*(1-E_put_C_grey) ///
						+ ce_diff_class*ce*(1-C_put_E_grey)*E_put_C_grey ///
						+ de_diff_class*de*D_put_E_grey*(1-E_put_D_grey) ///
						+ de_diff_class*de*(1-D_put_E_grey)*E_put_D_grey) ///
						/ (ab_diff_class + be_diff_class + cd_diff_class + ce_diff_class + de_diff_class) 
						
gen no_exch_between_2grey = .
replace no_exch_between_2grey = (ab_diff_class*ab*A_put_B_grey*B_put_A_grey ///
						+ be_diff_class*be*B_put_E_grey*E_put_B_grey ///
						+ cd_diff_class*cd*C_put_D_grey*D_put_C_grey /// 
						+ ce_diff_class*ce*C_put_E_grey*E_put_C_grey ///
						+ de_diff_class*de*D_put_E_grey*E_put_D_grey) ///
						/ (ab_diff_class + be_diff_class + cd_diff_class + ce_diff_class + de_diff_class) 

*exchange within class (same class)

gen exch_within = .
replace exch_within = (ab_same_class*AB + be_same_class*BE + cd_same_class*CD + ///
						ce_same_class*CE + de_same_class*DE) / (ab_same_class +  ///
						be_same_class + cd_same_class + ce_same_class + de_same_class) 

gen asy_exch_within = .
replace asy_exch_within = (ab_same_class*aB + ab_same_class*Ab + be_same_class*Be + be_same_class*bE ///
						+ cd_same_class*cD + cd_same_class*Cd + ce_same_class*cE + ce_same_class*Ce ///
						+ de_same_class*De + de_same_class*dE) / (ab_same_class +  ///
						be_same_class + cd_same_class + ce_same_class + de_same_class)

gen no_exch_within = .
replace no_exch_within = (ab_same_class*ab + be_same_class*be + cd_same_class*cd + ///
						ce_same_class*ce + de_same_class*de) / (ab_same_class +  ///
						be_same_class + cd_same_class + ce_same_class + de_same_class) 

gen asy_exch_within_grey = .
replace asy_exch_within_grey = (ab_same_class*aB*A_put_B_grey + ab_same_class*Ab*B_put_A_grey ///
						+ be_same_class*Be*E_put_B_grey + be_same_class*bE*B_put_E_grey ///
						+ cd_same_class*cD*C_put_D_grey + cd_same_class*Cd*D_put_C_grey ///
						+ ce_same_class*cE*C_put_E_grey + ce_same_class*Ce*E_put_C_grey ///
						+ de_same_class*De*E_put_D_grey + de_same_class*dE*D_put_E_grey) ///
						/ (ab_same_class + be_same_class + cd_same_class + ce_same_class + de_same_class)

gen no_exch_within_1grey = .
replace no_exch_within_1grey = (ab_same_class*ab*A_put_B_grey*(1-B_put_A_grey) ///
						+ ab_same_class*ab*B_put_A_grey*(1-A_put_B_grey) ///
						+ be_same_class*be*B_put_E_grey*(1-E_put_B_grey) ///
						+ be_same_class*be*(1-B_put_E_grey)*E_put_B_grey ///
						+ cd_same_class*cd*C_put_D_grey*(1-D_put_C_grey) /// 
						+ cd_same_class*cd*(1-C_put_D_grey)*D_put_C_grey /// 
						+ ce_same_class*ce*C_put_E_grey*(1-E_put_C_grey) ///
						+ ce_same_class*ce*(1-C_put_E_grey)*E_put_C_grey ///
						+ de_same_class*de*D_put_E_grey*(1-E_put_D_grey) ///
						+ de_same_class*de*(1-D_put_E_grey)*E_put_D_grey) ///
						/ (ab_same_class + be_same_class + cd_same_class + ce_same_class + de_same_class) 
						
gen no_exch_within_2grey = .
replace no_exch_within_2grey = (ab_same_class*ab*A_put_B_grey*B_put_A_grey ///
						+ be_same_class*be*B_put_E_grey*E_put_B_grey ///
						+ cd_same_class*cd*C_put_D_grey*D_put_C_grey /// 
						+ ce_same_class*ce*C_put_E_grey*E_put_C_grey ///
						+ de_same_class*de*D_put_E_grey*E_put_D_grey) ///
						/ (ab_same_class + be_same_class + cd_same_class + ce_same_class + de_same_class) 
						
********************************************************************************
* TABLE 1 Info exchange across and within classes
********************************************************************************					
				
sum exch_within
local avg1 =round(`r(mean)', 0.01)

sum no_exch_within
local avg2 =round(`r(mean)', 0.001)

sum asy_exch_within
local avg3 = round(`r(mean)', 0.001)

sum asy_exch_within_grey
local avg4 = round(`r(mean)', 0.001)

sum no_exch_within_1grey
local avg5 = round(`r(mean)', 0.001)

sum no_exch_within_2grey
local avg6 = round(`r(mean)', 0.001)

sum exch_between
local avg7 =round(`r(mean)', 0.001)

sum no_exch_between
local avg8 =round(`r(mean)', 0.001)

sum asy_exch_between
local avg9 =round(`r(mean)', 0.001)

sum asy_exch_between_grey
local avg10 = round(`r(mean)', 0.001)

sum no_exch_between_1grey
local avg11 = round(`r(mean)', 0.001)

sum no_exch_between_2grey
local avg12 = round(`r(mean)', 0.001)

matrix A0 = (`avg1', `avg3', `avg2' \ /// 
			     0 , `avg4', `avg5' \ ///
				 0 , 0,      `avg6' \ ///
			 `avg7', `avg9', `avg8' \  ///
			     0 , `avg10',`avg11' \ ///
				 0 , 0,      `avg12' )
						
matrix rownames A0 = "Within Class" "Within 1 grey" "Within 2 grey" "Across Classes" "Across Classes 1 grey" "Across Classes 2 grey" 
matrix colnames A0 =  Info_exch  Asy_exch No_exch
esttab matrix(A0) using "$results/Table1.tex", replace
restore
			
********************************************************************************
* TABLE 2 - INDIVIDUAL LEVEL REGRESSIONS
********************************************************************************
* Degree Centrality
gen degree_centrality=.
replace degree_centrality=0.25 if idnetwork==1 & pos=="a"
replace degree_centrality=0.5 if idnetwork==1 & pos=="b"
replace degree_centrality=0.5 if idnetwork==1 & pos=="c"
replace degree_centrality=0.5 if idnetwork==1 & pos=="d"
replace degree_centrality=0.75 if idnetwork==1 & pos=="e"

replace degree_centrality=0.5 if idnetwork==2 & pos=="a"
replace degree_centrality=0.5 if idnetwork==2 & pos=="b"
replace degree_centrality=0.5 if idnetwork==2 & pos=="c"
replace degree_centrality=0.5 if idnetwork==2 & pos=="d"
replace degree_centrality=1 if idnetwork==2 & pos=="e"

replace degree_centrality=0.25 if idnetwork==3 & pos=="a"
replace degree_centrality=0.75 if idnetwork==3 & pos=="b"
replace degree_centrality=0.75 if idnetwork==3 & pos=="c"
replace degree_centrality=0.5 if idnetwork==3 & pos=="d"
replace degree_centrality=0.75 if idnetwork==3 & pos=="e"

gen degree_cat = 0
replace degree_cat = 0 if degree_centrality== 0.25
replace degree_cat = 1 if degree_centrality== 0.5
replace degree_cat = 2 if degree_centrality== 0.75
replace degree_cat = 2 if degree_centrality== 1

* Betweenness Centrality
gen betweenness_centrality=.
replace betweenness_centrality=0 if idnetwork==1 & pos=="a"
replace betweenness_centrality=0.5 if idnetwork==1 & pos=="b"
replace betweenness_centrality=0 if idnetwork==1 & pos=="c"
replace betweenness_centrality=0 if idnetwork==1 & pos=="d"
replace betweenness_centrality=0.67 if idnetwork==1 & pos=="e"

replace betweenness_centrality=0 if idnetwork==2 & pos=="a"
replace betweenness_centrality=0 if idnetwork==2 & pos=="b"
replace betweenness_centrality=0 if idnetwork==2 & pos=="c"
replace betweenness_centrality=0 if idnetwork==2 & pos=="d"
replace betweenness_centrality=0.67 if idnetwork==2 & pos=="e"

replace betweenness_centrality=0 if idnetwork==3 & pos=="a"
replace betweenness_centrality=0.5 if idnetwork==3 & pos=="b"
replace betweenness_centrality=0.17 if idnetwork==3 & pos=="c"
replace betweenness_centrality=0 if idnetwork==3 & pos=="d"
replace betweenness_centrality=0.17 if idnetwork==3 & pos=="e"

gen bet_cat = .
replace bet_cat = 0 if betweenness_centrality== 0
replace bet_cat = 0 if float(betweenness_centrality)== float(0.17)
replace bet_cat = 1 if betweenness_centrality== 0.5
replace bet_cat = 2 if float(betweenness_centrality) == float(0.67)

label define categorie 0 "Low" 1 "Med" 2 "High"
label values bet_cat categorie
label values degree_cat categorie
xtset idusers phase

xtreg num_corrects  c.degree_centrality  i.idnetwork if grp_act_num==5 , re vce(robust)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table2.tex", replace tex(frag) nonotes  nocons keep(degree_centrality betweenness_centrality) label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

xtreg num_corrects  c.betweenness_centrality i.idnetwork if grp_act_num==5 , re vce(robust)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table2.tex", append tex(frag) nonotes  nocons keep(degree_centrality betweenness_centrality) label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

xtreg num_corrects  c.degree_centrality c.betweenness_centrality i.idnetwork if grp_act_num==5 , re vce(robust)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table2.tex", append tex(frag) nonotes  nocons keep(degree_centrality betweenness_centrality) label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')
		
*Define sample for multicollinearity, same degree, different betweenness			

gen sample_mult = 0 
replace sample_mult = 1 if idnetwork == 1 & pos=="b"
replace sample_mult = 1 if idnetwork == 1 & pos=="c"
replace sample_mult = 1 if idnetwork == 1 & pos=="d"

replace sample_mult = 1 if idnetwork == 3 & pos=="b"
replace sample_mult = 1 if idnetwork == 3 & pos=="c"
replace sample_mult = 1 if idnetwork == 3 & pos=="e"

*Define sample for multicollinearity, same betweenness, different degree			

gen sample_mult2 = 0 
replace sample_mult2 = 1 if idnetwork == 1 & pos=="a"
replace sample_mult2 = 1 if idnetwork == 1 & pos=="c"
replace sample_mult2 = 1 if idnetwork == 1 & pos=="d"

replace sample_mult2 = 1 if idnetwork == 3 & pos=="a"
replace sample_mult2 = 1 if idnetwork == 3 & pos=="d"

*regressions

xtreg num_corrects  c.degree_centrality i.idnetwork if grp_act_num==5  & sample_mult2==1, re vce(robust)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table2.tex", append tex(frag) nonotes  nocons keep(degree_centrality betweenness_centrality) label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

xtreg num_corrects c.betweenness_centrality i.idnetwork if grp_act_num==5  & sample_mult==1, re vce(robust)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table2.tex", append tex(frag) nonotes  nocons keep(degree_centrality betweenness_centrality) label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

********************************************************************************
* FIGURE 2 CORRECT GUESSES BY ROUND AND CENTRALITY LEVEL
********************************************************************************

set scheme plotplain
 
lgraph num_corrects phase if grp_act_num==5 	, err(sd) lop(recast(scatter)) by(degree_cat) separate(0.05) xlabel(1 2 3,valuel) ytitle("Correct Guesses") title("Degree Centrality", size(medium)) 
graph save "$results/degree", replace

lgraph num_corrects phase if grp_act_num==5 	, err(sd) lop(recast(scatter)) by(bet_cat) separate(0.05) xlabel(1 2 3,valuel) ytitle("Correct Guesses")  title("Betweenness Centrality", size(medium)) nolegend
graph save "$results/between", replace

grc1leg2 "$results/degree" "$results/between", col(2) ycommon   position(6) span lcols(3) ring(0)  lyoffset(-1) iscale(*1.15) legscale(2.5)
graph export "$results/Figure_2.pdf", replace


********************************************************************************
* TABLE 3 GROUP LEVEL REGRESSIONS
********************************************************************************

preserve

sort phase character
quietly by phase character:  gen dup = cond(_N==1,0,_n)
drop if dup>1
					
keep if  grp_act_num==5				

reghdfe grp_num_corrects   i.idnetwork  , vce(robust) 
gen sample=e(sample)
qui: sum grp_num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table3.tex", replace tex(frag) nonotes  nocons label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

reghdfe grp_num_corrects   i.idnetwork  i.phase  , vce(robust) 
gen sample=e(sample)
qui: sum grp_num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
drop sample
outreg2 using "$results/Table3.tex", append tex(frag) nonotes nocons  label  ctitle("Correct") ///
addtext( Sample Mean, `m1', Sample SD, `sd')

restore 	


********************************************************************************
* TABLE 4 CENTRAL PLAYER'S BEHAVIOR
********************************************************************************

*Col 1: Number of E's correct guesses by network
xtreg num_corrects i.idnetwork if grp_act_num==5  & pos_id==5, re 
local n2=round(_b[2.idnetwork], 0.001)
di `n2'
local n3=round(_b[3.idnetwork], 0.001)
gen sample=e(sample)
qui: sum num_corrects  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
local differenza = round(`n2'-`n3', 0.001)
drop sample
test 2.idnetwork=3.idnetwork
local fstat=round(`r(chi2)', 0.001)
local fpval=round(`r(p)', 0.001)
outreg2 using "$results/Table4.tex", replace tex(frag) nonotes   label  ctitle("Correct (E)") ///
addtext(Sample Mean, `m1', Sample SD, `sd', Network 2 vs. Network 3, `differenza', p-value, `fpval')

*Col 2: Probability that B, C or D got E's hint right
xtreg node_got_E_right i.idnetwork if grp_act_num==5  & (pos_id == 4 | pos_id==3 | pos_id==2), re 
local n2=round(_b[2.idnetwork], 0.001)
local n3=round(_b[3.idnetwork], 0.001)
gen sample=e(sample)
qui: sum node_got_E_right  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
local differenza = round(`n2'-`n3', 0.001)
drop sample
test 2.idnetwork=3.idnetwork
local fstat=round(`r(chi2)', 0.001)
local fpval=round(`r(p)', 0.001)
outreg2 using "$results/Table4.tex", append tex(frag) nonotes   label  ctitle("B,C,D got E") ///
addtext(Sample Mean, `m1', Sample SD, `sd', Network 2 vs. Network 3, `differenza', p-value, `fpval')

*Col 3: Probability that A or B's hints reach D or vice-versa
xtreg other_side_right i.idnetwork if grp_act_num==5  & (pos_id == 1 | pos_id==2 | pos_id==4) , re 
local n2=round(_b[2.idnetwork], 0.001)
local n3=round(_b[3.idnetwork], 0.001)
gen sample=e(sample)
qui: sum other_side_right  if sample==1
local m1=round(`r(mean)', 0.001)
local sd=round(`r(sd)', 0.001)
local differenza = round(`n2'-`n3', 0.001)
drop sample
test 2.idnetwork=3.idnetwork
local fstat=round(`r(chi2)', 0.001)
local fpval=round(`r(p)', 0.0001)
outreg2 using "$results/Table4.tex", append tex(frag) nonotes  dec(3) label  ctitle("A or B got D") ///
addtext(Sample Mean, `m1', Sample SD, `sd', Network 2 vs. Network 3, `differenza', p-value, `fpval')

**********************************************
* Table A1 - Number of active players per group
**********************************************

preserve

keep idnetwork phase character grp_act_num

sort phase character
quietly by phase character:  gen dup = cond(_N==1,0,_n)

drop if dup>1

foreach i of num  0/5 {

sum if grp_act_num==`i'
local n_`i' = r(N)

}

matrix A0 = ( `n_5' , `n_4' , `n_3' , `n_2' , `n_1' , `n_0' )

foreach j of num  1/3 {

	foreach i of num  0/5 {

	sum if grp_act_num==`i' & idnetwork ==`j'
	local n_`i' = r(N)

	}

	matrix A`j' = ( `n_5' , `n_4' , `n_3' , `n_2' , `n_1' , `n_0' )

}



mat B = A0 \ A1 \ A2 \ A3  
matrix rownames B = "All" "Network 1" "Network 2" "Network 3"
matrix colnames B =  Five Four Three Two One Zero
esttab matrix(B) using "$results/Table_A1.tex", replace

restore
